//
// Created by xusun on 2022/1/28.
//
#include<iostream>

using namespace std;

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
struct ListNode {
    int val;
    ListNode *next;

    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    bool hasCycleOpt(ListNode *head) {
        while (head) {
            if (head->val == 10002)return true;
            else head->val = 10002;
            head = head->next;
        }
        return false;
    }

    bool hasCycle(ListNode *head) {
        if (head == NULL)return false;
        if (head->next == head) return true;
        int count = 10001;
        ListNode *tmp = head;
        while (count--) {
            if (tmp == NULL)return false;
            tmp = tmp->next;
        }
        return true;
    }
};